package com.elookinto.importing;



/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import org.apache.jackrabbit.core.jndi.RegistryHelper;
import org.apache.jackrabbit.extractor.DefaultTextExtractor;
import org.apache.jackrabbit.rmi.repository.RMIRemoteRepository;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParsingReader;
import org.apache.tika.config.TikaConfig;
import org.apache.tika.metadata.Metadata;

import javax.jcr.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.io.FileInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Calendar;
import java.util.Set;

import sun.net.www.MimeTable;

/**
 * The First Steps example class.
 */
public class Import {

    /**
     * Run the Filesystem Import example.
     *
     * @param args command line arguments
     */
    public static void main(String[] args) {
        args = new String[]{"C:\\zhouwu\\tesdocs1"};
        try {
            Repository repository = getRepository();
            SimpleCredentials creds = new SimpleCredentials("guest", "".toCharArray());
            Session session = repository.login(creds);
            Node root = session.getRootNode();

            if (args.length>0) {
                File startingpoint=new File(args[0]);
                importFolder (root.addNode(startingpoint.getName(), "nt:folder"), startingpoint);
                session.save();
                dump(root.getNode(startingpoint.getName()));
            } else {
                System.err.println("Please specify a starting point for the file system import into the repository.");
            }


        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e);
        }
    }

    /**
     * Creates a Repository instance to be used by the example class.
     *
     * @return repository instance
     * @throws Exception on errors
     */
    private static Repository getRepository() throws Exception {
        /*String configFile = "C:\\zhouwu\\apache-tomcat-6.0.20\\bin\\jack\\repository.xml";
        String repHomeDir = "C:\\zhouwu\\apache-tomcat-6.0.20\\bin\\jack";

        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY,
                "org.apache.jackrabbit.core.jndi.provider.DummyInitialContextFactory");
        env.put(Context.PROVIDER_URL, "localhost");
        InitialContext ctx = new InitialContext(env);

        RegistryHelper.registerRepository(ctx, "jack", configFile, repHomeDir, true);
         */


Repository repository =
    new RMIRemoteRepository("//localhost/jackrabbit.repository");

        return   repository;
    }

    /**
     * Imports a File.
     *
     * @param parentnode Parent Repository Node
     * @param file File to be imported
     * @throws RepositoryException on repository errors, IOException on io errors
     */

     private static  AutoDetectParser PARSER;
    private static  String[] TYPES;

     static {
        InputStream stream =
            DefaultTextExtractor.class.getResourceAsStream("tika-config.xml");
        try {
            try {
                PARSER = new AutoDetectParser(new TikaConfig(stream));

                Set<String> types = PARSER.getParsers().keySet();
                TYPES = types.toArray(new String[types.size()]);
            } finally {
                stream.close();
            }
        } catch (Exception e) {
            throw new RuntimeException(
                    "Unable to load Tika configuration", e);
        }
    }
    public static void importFile(Node parentnode, File file) throws RepositoryException, IOException {

        MimeTable mt = MimeTable.getDefaultTable();
        String mimeType = mt.getContentTypeFor(file.getName());
        if (mimeType==null) mimeType="application/octet-stream";



        Node fileNode = parentnode.addNode(file.getName(), "nt:file");
        Node resNode = fileNode.addNode("jcr:content", "nt:resource");
        resNode.setProperty("jcr:mimeType", mimeType);
        resNode.setProperty("jcr:encoding", "");
        Binary b;
        //resNode.setProperty("jcr:data", )
        resNode.setProperty("jcr:data", new java.net.URL("http://localhost:8080/jack/remote.jsp").openStream());
                //FileInputStream(file));
                                             Metadata metadata = new Metadata();
        /*if (type != null && type.trim().length() > 0) {
            metadata.set(Metadata.CONTENT_TYPE, type.trim());
        } */
        // TODO: This creates a background thread. Is that a problem?
           // Metadata metadata = new Metadata();
        Object o = new ParsingReader(PARSER, new FileInputStream(file) , metadata);

        for (String ss: metadata.names())
        {
            System.out.println(ss);
             for (String sss: metadata.getValues(ss))
                System.out.println("..." + sss );
        };

        Calendar lastModified = Calendar.getInstance();
        lastModified.setTimeInMillis(file.lastModified());
        resNode.setProperty("jcr:lastModified", lastModified);
        System.out.println(fileNode.getPath());
   }


    /**
     * Import a Folder.
     *
     * @param parentnode Parent Repository Node
     * @param directory Directory to be traversed
     * @throws RepositoryException on repository errors, IOException on io errors
     */

    private static void importFolder(Node parentnode, File directory) throws RepositoryException, IOException  {
        File[] direntries = directory.listFiles();
        System.out.println(parentnode.getPath());
        for (int i=0; i<direntries.length; i++) {
            File direntry = direntries[i];
            if (direntry.isDirectory()) {
                Node childnode = parentnode.addNode(direntry.getName(),"nt:folder");
                importFolder(childnode, direntry);
            } else {
                importFile(parentnode, direntry);
            }
        }
    }

    /**
     * Dumps the contents of the given node to standard output.
     *
     * @param node the node to be dumped
     * @throws RepositoryException on repository errors
     */
    public static void dump(Node node) throws RepositoryException {
        System.out.println(node.getPath());

        PropertyIterator properties = node.getProperties();
        while (properties.hasNext()) {
            Property property = properties.nextProperty();
            System.out.print(property.getPath() + "=");
            if (property.getDefinition().isMultiple()) {
                Value[] values = property.getValues();
                for (int i = 0; i < values.length; i++) {
                    if (i > 0) {
                        System.out.println(",");
                    }
                    System.out.println(values[i].getString());
                }
            } else {
                if (property.getType()==PropertyType.BINARY) {
                    System.out.print("<binary>");
                }  else {
                    System.out.print(property.getString());
                }

            }
            System.out.println();
        }

        NodeIterator nodes = node.getNodes();
        while (nodes.hasNext()) {
            Node child = nodes.nextNode();
            dump(child);
        }
    }
}



